package com.novoda.runbuddy.view.overlay;

import java.util.ArrayList;
import java.util.List;

import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.RectF;
import android.graphics.Point;
import android.location.Location;
import android.view.View;

import com.google.android.maps.GeoPoint;
import com.google.android.maps.Overlay;
import com.google.android.maps.MapView;
import com.google.android.maps.Projection;

import com.novoda.runbuddy.activity.MapOfRuns;


public class MapOfJogsOverlay extends Overlay {

	private final android.graphics.Point	PIN_HOTSPOT		= new android.graphics.Point(5, 29);
	private final MapOfRuns					mapOfJogActivity;

	@SuppressWarnings("unused")
	protected final Bitmap					PIN_START;
	protected final Bitmap					PIN_END;
	private int[]							prevScreenCoords;
	public List<Location>					path			= new ArrayList<Location>();
	int[]									screenCoords	= { 0, 0 };
	private Canvas							canvas;
	boolean									firstInPath;
	boolean									first			= true;

	public MapOfJogsOverlay(MapOfRuns map, Bitmap startLocationImage, Bitmap endLocationImage, Bitmap infoImageBG) {
		mapOfJogActivity = map;
		PIN_START = startLocationImage;
		PIN_END = endLocationImage;

		prevScreenCoords = new int[2];
	}

	public void draw(Canvas canvas, MapView mapView, boolean withShadows) {
		super.draw(canvas, mapView, withShadows);
		this.canvas = canvas;
		
		int entry = 1;

		for (Location loc : path) {
			screenCoords = getPointOnMap(mapView, (int) loc.getLatitude(), (int) loc.getLongitude());

			if (entry == 1) {
				drawFirstPoint(screenCoords[0], screenCoords[1]);
				entry++;
			} else {
				drawLineToPoint(screenCoords[0], screenCoords[1]);
			}

		}

		dropPinMarker(canvas, PIN_START, screenCoords[0] - 3, screenCoords[1] + 20);

	}

	View.OnClickListener	GoClickListener	= new View.OnClickListener()
											{
												public void onClick(View arg0) {
													mapOfJogActivity.finish();
												}
											};

	public void drawFirstPoint(int lat, int longi) {
		dropPinMarker(canvas, PIN_END, lat, longi - 18);
		canvas.drawOval(new RectF(lat - 5, longi + 5, lat + 5, longi - 5), getJoggingPoint());

		prevScreenCoords[0] = lat;
		prevScreenCoords[1] = longi;
	}

	public void drawLineToPoint(int lat, int longi) {
		canvas.drawOval(new RectF(lat - 5, longi + 5, lat + 5, longi - 5), getJoggingPoint());
		canvas.drawLine(prevScreenCoords[0], prevScreenCoords[1], lat, longi, getJoggingPathPainter());

		prevScreenCoords[0] = lat;
		prevScreenCoords[1] = longi;
	}

	private Paint getJoggingPoint() {
		Paint paint = new Paint();
		paint.setAntiAlias(true);
		paint.setARGB(80, 156, 192, 36);
		paint.setStrokeWidth(3);
		paint.setStyle(Paint.Style.FILL_AND_STROKE);
		return paint;
	}

	private Paint getJoggingPathPainter() {
		Paint paint = new Paint();
		paint.setAntiAlias(true);
		paint.setARGB(80, 255, 0, 0);
		paint.setStrokeWidth(3);
		paint.setStyle(Paint.Style.FILL_AND_STROKE);
		return paint;
	}

	private int[] getPointOnMap(MapView mapView, int lat, int longi) {
		GeoPoint point = new GeoPoint(lat, longi);
		int[] screenCoords = new int[2];
		Projection p = mapView.getProjection();
		
		Point screenPoint = new Point();
		p.toPixels(point, screenPoint);
		
		screenCoords[0] = screenPoint.x;
		screenCoords[1] = screenPoint.y;
		
		return screenCoords;
	}

	private void dropPinMarker(Canvas canvas, Bitmap image, int lat, int longi) {
		canvas.drawBitmap(image, lat - PIN_HOTSPOT.x, longi - PIN_HOTSPOT.y, getRoutePathPainter());
	}

	protected Paint getJourneyTimePainter() {
		Paint paint = new Paint();
		paint.setAntiAlias(true);
		paint.setARGB(255, 255, 255, 255);
		paint.setStrokeWidth(1);
		paint.setStyle(Paint.Style.FILL_AND_STROKE);
		paint.setTextSize(24);
		return paint;
	}

	protected Paint getJourneyDistPainter() {
		Paint paint = new Paint();
		paint.setAntiAlias(true);
		paint.setARGB(255, 255, 255, 255);
		paint.setStrokeWidth(1);
		paint.setStyle(Paint.Style.FILL_AND_STROKE);
		paint.setTextSize(16);
		return paint;
	}

	protected Paint getRoutePathPainter() {
		Paint paint = new Paint();
		paint.setAntiAlias(true);
		paint.setStrokeWidth(4);
		paint.setARGB(100, 113, 105, 252);
		paint.setStyle(Paint.Style.STROKE);
		return paint;
	}

	public void addLocationToPath(Location pointLocation) {
		path.add(pointLocation);
	}

}
